home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / xComputer 1.2 / READ ME (with instructions!) next >
Text File  |  1996-01-03  |  22KB  |  426 lines

  1.  
  2.                                                   (January 3, 1996)
  3.  
  4.  
  5. xComputer 1.2:  A "visible computer" that simulates a computer
  6.                 with a simple CPU and memory.  It can be programmed
  7.                 in assembly language, and the execution of the
  8.                 programs can be followed in detail.
  9.  
  10. by:  David Eck
  11.      Department of Mathematics and Computer Science
  12.      Hobart and William Smith Colleges
  13.      Geneva, NY   14456
  14.      (E-mail:  eck@hws.edu)
  15.      (Home page:  http://hws3.hws.edu:9000/eck/index.html)
  16.      
  17. Note: This program was written primarily to be used with my textbook,
  18.       _The Most Complex Machine: A Survey of Computers and Computing_.
  19.       (Even without the book, though, it can be useful in helping
  20.       you learn about how computers work.)  More information
  21.       about the text, along with several other free programs for
  22.       Macintosh computers and a complete lab manual, is available
  23.       at the following URL on the World Wide Web:
  24.       
  25.                      http://godel.hws.edu/TMCM.html
  26.  
  27. Restrictions:   This program can be freely distributed for private,
  28.      non-commercial use.  This includes distribution in software archives,
  29.      bulletin boards, and CD-ROM software collections.  I do ask, however,
  30.      that the program not be adopted for use in a computer science course
  31.      unless my textbook is also being used for that course.
  32.  
  33.  
  34. This file contains information about using the xComputer.  It should
  35. be accompanied by the program, xComputer 1.2, by a folder of example
  36. programs, and by an order form for _The Most Complex Machine_.
  37.  
  38.  
  39.  
  40. Part 1:  xComputer Basics
  41. -------------------------
  42.  
  43. The main window for the program contains three sections:  The Console,
  44. the Memory, and the Registers.  The Memory is a scrolling list that 
  45. displays 1024 memory locations, numbered from 0 to 1023.  Each location
  46. contains a 16-bit number that can be interpreted either as data or
  47. as a machine language instruction.  The Registers section of the
  48. window shows eight registers that are components in the xComputer's
  49. Central Processing Unit.  The registers are explained below.  Finally,
  50. the Console is for interacting with and controlling the xComputer.
  51. For example, program instructions and data can be entered into
  52. memory using the Console.
  53.  
  54. You can also have program windows.  A program window can contain
  55. an assembly language program that can be assembled and loaded into 
  56. memory by using the Load command in the Assembler menu.  You should
  57. think of this as a shortcut for typing the program one instruction
  58. at a time on the Console.  Another advantage of a program window
  59. is that the program you type into it can be saved in a file and later
  60. re-opened using the Open command in the File menu.
  61.  
  62. Very simple programs can be entered directly into the console.
  63. Make sure that the box labeled "addr:" contains the address of the
  64. location in memory where you want to put the first program
  65. instruction.  (This is usually location zero.)  Type the instructions
  66. into the box labeled "data:", one at a time; after typing each
  67. instruction, press return or click on the "Load Memory" button.
  68. If the instruction you typed was legal, it will be entered into
  69. memory and the number in the "addr" box will be incremented.
  70. For example, you could enter the following program to add 17 to
  71. 105 and store the answer in location 10:
  72.  
  73.                 lod-c 105
  74.                 add-c 17
  75.                 sto 10
  76.                 hlt
  77.  
  78. (With the default menu settings, you won't actually see these 
  79. instructions in memory.  The instructions are translated into
  80. machine language and are stored in memory as numbers.  However,
  81. the instructions are there and will be executed correctly when
  82. you run the program.)
  83.  
  84. To run the program, first check that the value in the PC register
  85. is the address of the location that contains the first instruction of
  86. the program.  The PC, or program counter, tells the xComputer which
  87. instruction to execute.  You can set its value to zero using the
  88. "Set PC=0" command in the Assembler menu.  To set it to some other
  89. value, enter the value in the "addr" box and then click on the
  90. "Load PC" button.  THIS IS IMPORTANT.  The most common and most
  91. frustrating mistake when trying to run a program on xComputer is
  92. simply to forget to tell xComputer where in memory the program is
  93. located!
  94.  
  95. Once you have set the PC, click on the checkbox labeled "Stop-Clock".
  96. As long as this box is unchecked, the computer is running.  If you
  97. prefer, you can use the Run command in the Assembler menu to start
  98. the computer running.  As the program  runs, you will see the
  99. values in the register changing.  The instructions are loaded one-by-one
  100. into the IR, or instruction register, and executed.  Soon, the program
  101. halts with the answer in memory location 10.
  102.  
  103. Here is a summary of what the registers are for and how the xComputer
  104. works:  The computer works by "fetching" an instruction from memory
  105. and then "executing" that instruction.  It takes the computer several
  106. small operations to fetch and execute each instruction from memory.
  107. These small steps are counted off by the number in the COUNT register.
  108. A fetch-and-execute cycle starts when the value in the COUNT register
  109. changes from 0 to 1.  Each time the value changes, another small step
  110. of the cycle takes place.  At the end of a fetch-and-execute cycle,
  111. one instruction has been fetched from memory and executed, and the
  112. value in the COUNT register is reset to zero to get ready for fetching
  113. and executing the next instruction.   This continues until a HLT instruction
  114. is executed (or until you check the "Stop-Clock" button).
  115.  
  116. What the computer does at each step is determined entirely by the
  117. contents of several registers:  The COUNT register, the IR (instruction
  118. register) and the AC (accumulator).  Everything is done mechanically:
  119. the numbers in these registers cause certain "control wires" to
  120. be turned on and off, and this in turn causes numbers to be moved from 
  121. place to place, sums to be computed, etc.  For details, see Chapter 3 of
  122. the textbook, _The Most Complex Machine_.  
  123.  
  124. The individual registers work as follows:
  125.  
  126.       ADDR register:  The address register holds the address of a
  127.           location in memory.  This is the location that can be
  128.           stored into or read from.
  129.           
  130.       PC register:  The program counter contains the address in memory
  131.           of the next program instruction to be executed.  The PC is
  132.           ordinarily incremented by 1 during each fetch-and-execute cycle.
  133.           (A JMP instruction simply changes the value stored in the PC,
  134.           so that the computer "jumps" to a different location in the
  135.           program.)
  136.           
  137.       IR register:   The instruction register holds a program instruction
  138.           while it is being executed.  This is where an instruction is
  139.           put when it is "fetched" from memory.
  140.           
  141.       COUNT register:  As explained above, the COUNT register counts
  142.           off the individual operations in each fetch-and-execute cycle.
  143.           
  144.       AC register:  The accumulator holds a number that is being used
  145.           in the current calculation.  When a number is loaded from
  146.           memory, it is put in the AC.  When a number is "added", it
  147.           is added to the value currently in the AC, and the result is
  148.           put back into the AC.  Etc.
  149.           
  150.       FLAG register:  The flag register contains a single bit that
  151.           can give extra information about a calculation.  For example,
  152.           when two 16-bit numbers are added, the final "carry" into
  153.           the 17-th column is stored in the FLAG register.  When a
  154.           shift operation is performed on the AC, the extra bit that
  155.           is shifted off the end is placed into the FLAG register.
  156.           
  157.       X and Y registers:  These hold numbers that are to be used in
  158.           a calculation.  For example, when two numbers are to be
  159.           added, they are placed into X and Y.  (The Y register is
  160.           also used as a temporary storage place in a few cases.)
  161.           
  162.  
  163.  
  164. Part 2:  Menu Operations
  165. ------------------------
  166.  
  167. The commands in the File, Edit and Windows menus should be
  168. reasonably self-explanatory.
  169.  
  170. The Assembler menu starts with two commands for loading programs
  171. into memory.  THESE CAN ONLY BE USED IF THE xComputer IS NOT RUNNING.
  172. The first command is used to load a program from the frontmost
  173. window on the screen.  The Load File command lets you select
  174. a file so that you can load its contents into the xComputer's
  175. memory without first opening the file in its own window.
  176.  
  177. The Run and Stop Clock commands are equivalent to unchecking and
  178. checking the Stop-Clock box in the xComputer window.
  179.  
  180. The two Disassemble commands will take the current contents of
  181. memory and turn them into an assembly language program in a 
  182. window.  Loading that window into memory will restore the contents
  183. of memory exactly.
  184.  
  185. The Clear Memory and Set PC=0 commands are self-evident.
  186.  
  187. The Options menu controls many details of how the xComputer runs and
  188. what you see on the screen as it runs.  It contains three sub-menus and
  189. three other options that can be turned off and on.
  190.  
  191. The Run Speed sub-menu determines how fast the xComputer runs.
  192. As the fastest speed, the registers are hidden so that the computer
  193. can run as fast as possible.  At the two slowest speeds, the
  194. computer doesn't really "run" by itself at all.  Instead, a
  195. button labeled "Next" appears in the Console, and you have to
  196. click repeatedly on this button to make the computer run.  This
  197. allows you to observe the operation of xComputer in great detail.
  198.  
  199. The Memory Display sub-menu determines what you see in the Memory
  200. area of the xComputer window.  You can see the contents of each
  201. memory location as a binary number, a signed integer (in the
  202. range -32768 to 32767), an unsigned integer (in the range 0 to
  203. 65535), an assembly language instruction, or as a pair of ASCII
  204. characters.  A "Graphics" display option will show the entire memory
  205. at once, as a grid of pixels.  That is, each of the 16-times-1024 bits
  206. in memory will be represented as a pixel that is off if the bit is
  207. zero and is on if the bit is one.  (It can be particularly effective
  208. to run the computer at full speed and watch the bits "dance" in
  209. the Graphics display.)  The final option in the Memory Display menu
  210. is "Show Control Wires Instead".  With this option, the Memory
  211. display is replaced by a list of the xComputer's control wires.
  212. (Turning control wires on and off is what makes things happen
  213. in the xComputer.)  As a program is executed, the control wires that
  214. are turned on at any given time are hilited.  You might not find this
  215. particularly useful unless you have read _The Most Complex Machine_.
  216.  
  217. The Register Display sub-menu is similar to the Memory Display
  218. menu, but not as useful.  It should be self-evident.
  219.  
  220. If the Autoscroll Memory option is turned on, then each time the
  221. value in the ADDR register is changed, the memory will be automatically
  222. scrolled so that the indicated memory location is lined up with the
  223. ADDR register.  This might make it easier for you to see how numbers
  224. are loaded from and stored into memory.
  225.  
  226. Not every 16-bit number represents a legal assembly language instruction.
  227. Ordinarily, xComputer simply ignores illegal instructions.  If you 
  228. turn on the Halt on Illegal Instruction option, however, xComputer will
  229. stop whenever it loads an illegal instruction into the IR register.
  230. This might make it easier for you to "debug" your programs.
  231.  
  232. The "Use I/O Services" option essentially modifies the xCompuer so that
  233. it can do simple input and output operations.  When you turn on this
  234. option, an "Output" list appears in the Console.  Then, when you
  235. run a program, an "Input queue" appears as well.  Several additional
  236. assembly language instructions become available when I/O services are on.
  237. The purpose is to illustrate several advanced aspects of computer
  238. operation in a simplified form.  This is explained in more detail
  239. below and in the sample programs.
  240.  
  241.  
  242.  
  243. Part 3:  The Assembly Language of xComputer 
  244. -------------------------------------------
  245.  
  246. A program for the xComputer consists of instructions and data.  Since
  247. everything is actually stored in memory in the form of 16-bit numbers,
  248. the only real distinction between instructions and data is the way
  249. they are used.  You should really think in terms of having several
  250. different ways of writing 16-bit numbers.  Any of the following ways
  251. are valid anywhere in a program:
  252.  
  253.     -- An assembly language instruction, from the list of instructions
  254.        given below.  (The instruction itself is represented in the
  255.        leftmost 6 bits of the 16-bit number.  The remaining 10 bits
  256.        specify a number between 0 and 1023 that is used as a 
  257.        parameter for the instruction.)
  258.        
  259.     -- A number in the range -32768 to 65535.  16-bit numbers can be used
  260.        to represent either unsigned integers in the range 0 to 65535 or
  261.        signed integers in the range -32768 to 32767.  It's just a matter
  262.        of interpretation.  You can enter numbers in either form.
  263.        
  264.     -- A binary number consisting of a B followed by from 1 to 16 bits.
  265.        For example:  B101110100111.  (This is the most "realistic"
  266.        representation.)
  267.        
  268.     -- A character preceded by a single left quote, such as:  'z
  269.        This represents the ASCII code of the character.
  270.        
  271. You can type any of these things into the "data" box in xComputer's Console.
  272. You can also use them in programs typed into program windows.  (Only one
  273. instruction is allowed per line.)
  274.  
  275. Programs in windows can contain several other features.  A semicolon and
  276. everything following it on a line is considered a comment and is ignored
  277. when the program is loaded.  Any program item can be preceded by a label and
  278. a colon.  The label, which consists of letters and digits beginning with a
  279. letter, then becomes a name for the memory location that contains that item
  280. when the program is loaded.  In fact, the label is treated as a NUMBER that
  281. gives the address of that location, and it can be used in a program wherever
  282. a number could be used. For example:
  283.  
  284.             start:   lod-c 0      ; "start" is a label
  285.                      lod-c start  ; loads the number "start" into the AC
  286.                      jmp start    ; jumps to location "start"
  287.                      
  288. A program item can be preceded by a number followed by a #.  This is a
  289. repetition count and is the same as typing the item the specified number
  290. of times.  For example, "25# 17"  puts a 17 in each of the next 25
  291. memory locations.  "4# SHL" is equivalent to four SHL instructions.
  292.  
  293. Ordinarily, a program is loaded into consective memory locations starting at
  294. location 0.  However, you can specify where loading is to take place
  295. with a @ followed by an address.  For example, "@100" specifies that the
  296. next item is to go into memory location 100.  (Items following that one
  297. will then go into location 101, 102, etc.)   A special form of this
  298. command, "@PC" is provided to load a number into the PC register.  THIS
  299. IS DONE ONLY ONCE, WHEN THE PROGRAM IS LOADED.  For example, "@PC 100"
  300. will put a 100 into the PC when the program is loaded.  Presumably,
  301. you would use this because your program starts at location 100, so you
  302. would like the PC to start with that value.
  303.  
  304. Here is a list of the assembly language instructions that can be
  305. used for the xComputer.  In these instructions, X represents a
  306. number between 0 and 1023.  In a program, X can be given as an
  307. ordinary decimal number, as a binary number preceded by a B
  308. (for example:  B1011), or as a single ASCII character preceded by
  309. a right single quote mark (for example:  'A  or  '?).  It can also
  310. be given as a symbolic label, provided that the name is used
  311. somewhere in the program as a label for a memory location.  If a
  312. number is given for an instruction that does not require it,
  313. such as HLT or SHL, the number is ignored.  Note that no distinction
  314. is made between upper and lower case in instruction names or labels.
  315.  
  316.      Instruction         Meaning
  317.     --------------  ------------------------------------------------
  318.     
  319.        ADD X          Add the number in memory location X to the AC 
  320.        ADD-C X        Add the number X to the AC
  321.        ADD-I X        Let Y be the contents of memory location X, and
  322.                           add the number in location Y to the AC 
  323.                           
  324.        SUB X          Subtract the number in memory location X from the AC 
  325.        SUB-C X        Subtract the number X from the AC
  326.        SUB-I X        Let Y be the contents of memory location X, and
  327.                           subtract the number in location Y from the AC
  328.                           
  329.        AND X          Bitwise AND the number in memory location X with the AC 
  330.        AND-C X        Bitwise AND the number X with the AC
  331.        AND-I X        Let Y be the contents of memory location X, and
  332.                           bitwise AND the number in location Y with the AC
  333.                           
  334.        OR X           Bitwise OR the number in memory location X with the AC 
  335.        OR-C X         Bitwise OR the number X with the AC
  336.        OR-I X         Let Y be the contents of memory location X, and
  337.                           bitwise OR the number in location Y with the AC
  338.                           
  339.        NOT            Apply a bitwise NOT to the AC
  340.        INC            Add 1 to the AC
  341.        DEC            Subtract 1 from the AC
  342.        SHL            Shift the AC left one bit
  343.        SHR            Shift the AC right one bit
  344.        
  345.        LOD X          Load the number in location X into the AC
  346.        LOD-C X        Load the number X into the AC
  347.        LOD-I X        Let Y be the contents of the memory location X, and
  348.                           load the number from location Y into the AC
  349.                           
  350.        STO X          Store the value in AC into memory location X
  351.        STO-I X        Let Y be the contents of memory location X, and
  352.                           store the value in AC into location Y
  353.  
  354.        JMP X          Jump to location X (that is, store X into the PC,
  355.                           so that the next instruction will be loaded from X)
  356.        JMP-I X        Let Y be the contents of memory location X, and
  357.                           jump to location Y
  358.  
  359.        JMZ X          If the value in the AC is zero, then jump to location X 
  360.        JMP-I X        If the value in the AC is zero, then let Y be the contents
  361.                           of memory location X, and jump to location Y
  362.  
  363.        JMN X          If the value in the AC is negative, then jump to
  364.                           location X
  365.        JMN-I X        If the value in the AC is negative, then let Y be the
  366.                           contents of memory location X, and jump to location Y
  367.  
  368.        JMF X          If the value in the FLAG register is one, then jump to
  369.                           location X
  370.        JMF-I X        If the value in the FLAG register is one, then let Y be the
  371.                           contents of memory location X, and jump to location Y
  372.                           
  373.        HLT            Halt.  That is, stop the xComputer by turning on the 
  374.                           STOP CLOCK control wire
  375.                           
  376.  
  377. In addition, when the "Use I/O Services" option is on, the following
  378. instructions can be used:
  379.  
  380.  
  381.        GTC       Get Character.  When the xComputer is running with I/O
  382.                       services on and the user types a character, that
  383.                       character is put into an "input queue".  The GTC
  384.                       instruction removes the next character from the 
  385.                       queue and puts its ASCII code into the AC register.
  386.                       If no character is available in the queue, a
  387.                       zero is put into the AC.
  388.        
  389.        PTC       Put Character.  Writes the contents of the AC register to
  390.                       the output list (which appears in the Console of the
  391.                       xComputer window when I/O services are on) in the form
  392.                       of a character.  If the number in the AC is greater than 
  393.                       255, then it is written as two characters.  
  394.        PTI       Put Integer.  Writes the contents of the AC register to
  395.                      the output list in the form of a signed integer.
  396.        PTU       Put Unsigned.  Writes the contents of the AC to the output
  397.                      list in the form of an unsigned integer.
  398.        PTB       Put Binary.  Writes the contents of the AC to the output
  399.                      list in the form of a binary number.
  400.        
  401.        INH X     Interrupt Handler at X.  When the user types a character,
  402.                      a "keyboard interrupt" is generated.  By default, the
  403.                      xComputer "handles" the keyboard interrupt by placing the
  404.                      character into the input queue.  The INH instruction can
  405.                      be used to set up further processing of keyboard
  406.                      interrupts.  Specifically, if an INH X instruction has
  407.                      been executed, when a keyboard interrupt occurs, the
  408.                      xComputer will save the state of the current computation,
  409.                      place the character into the keyboard queue, and jump
  410.                      to location X.  It will execute instructions starting
  411.                      at location X until an RTI instruction is encountered.
  412.                      Then, it will restore the saved state and pick up 
  413.                      where it left off when the interrupt occured.  (Unless,
  414.                      that is, another key has been pressed -- in that case,
  415.                      it will handle the "pending interrupt" by going back
  416.                      to location X to handle the next character.  Note that
  417.                      an interrupt handler MUST remove a character from the
  418.                      input queue with a GTC command and MUST end with an
  419.                      RTI instruction.)  The command "INH 1023" can be used
  420.                      to CANCEL the current interrupt handler and restore the
  421.                      default behavior.
  422.  
  423.        RTI       Return from Interrupt.  Ends a keyboard interrupt handler,
  424.                      as explained above under the INH command.
  425.  
  426.